<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/13
 * Time: 13:02
 */
namespace App\Http\Controllers;



use App\Common\Base;
use App\Service\BannerService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\DataReturnService as DRS;

class BannerController extends Controller
{
    /**
     * @Notes:获取banner详情
     * @param Request $request
     * @return array
     * @author: LiBaoJin
     * @Time:
     */
    public function main_banner(Request$request){
        $postData  = ['token' => $request->post('token')];
        $validator = Validator::make($postData,
            [
                'token'         => 'required|string',
            ]);
        if ($validator->fails()) {
            return DRS::returnError(1000,'验证错误！',$validator->errors());
        }
        $userInfo = Base::getUserByToken($postData['token']);
        if($userInfo['code'] != '666'){
            return $userInfo;
        }
        $user   = $userInfo['data'];
        $banner_list  = Db::table('banner')
            ->where(['business_id'=>$user['business_id'],'state'=>0])
            ->first();

       $banner_list  = (array)$banner_list;
        if(empty($banner_list)){
            return   DRS::returnOk([''],'获取banner列表活动成功');
        }
        $product_id = json_decode($banner_list['product_ids']);
        $banner_list['products'] =[''];
        if(!empty($product_id)){
            $banner_list['products'] = BannerService::banner_product_list($product_id);
        }
       return  DRS::returnOk([$banner_list],'获取banner列表活动成功');;
    }

    public function banner_list(Request$request){
        $postData  = ['token' => $request->post('token')];
        $validator = Validator::make($postData,
            [
                'token'         => 'required|string',
            ]);
        if ($validator->fails()) {
            return DRS::returnError(1000,'验证错误！',$validator->errors());
        }
        $userInfo = Base::getUserByToken($postData['token']);
        if($userInfo['code'] != '666'){
            return $userInfo;
        }
        $user   = $userInfo['data'];
        $banner_list  = Db::table('banner')
            ->where(['business_id'=>$user['business_id'],'state'=>0])
            ->first();

        $banner_list  = (array)$banner_list;
        if(empty($banner_list)){
            return   DRS::returnOk([''],'获取banner列表活动成功');
        }
        $banner_pic  = Db::table('banner_pictures')
            ->where(['banner_id'=>$banner_list['id'],'state'=>0])
            ->get()
            ->toArray();
        $banner_pic = array_map('get_object_vars', $banner_pic);
        $banner_list['banner_pic'] = $banner_pic;
        return  DRS::returnOk([$banner_list],'获取banner列表活动成功');;
    }

}

